package com.my.screenadapter.alarmclock;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import java.util.Calendar;
import android.os.Bundle;
import android.os.SystemClock;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;

import com.my.screenadapter.R;

import java.util.Locale;
import java.util.TimeZone;

/**
 * Created by 张英杰 on 2016/12/15.
 */
public class AlarmClockActivity extends AppCompatActivity {
    private int mHour = -1;
    private int mMinute = -1;
    private TimePicker mTimePicker;
    private Button mButton1;
    private Button mButton2;
    private Button mButtonCancel;
    public static final long DAY = 1000L * 60 * 60 * 24;
    private static final String TAG = "AlarmClockActivity";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarmclock);
        Log.i(TAG, "onCreate: ===testbbb");
        startAlarmClock();
    }

    private void startAlarmClock() {
        java.util.Calendar calendar = java.util.Calendar.getInstance(Locale.CHINA);
        if (mHour == -1 && mMinute == -1) {
            mHour = calendar.HOUR_OF_DAY;
            mMinute = calendar.MINUTE;
        }
        mTimePicker = (TimePicker) findViewById(R.id.timePicker);
        mTimePicker.setCurrentHour(mHour);
        mTimePicker.setCurrentMinute(mMinute);
        mTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                mHour = hourOfDay;
                mMinute = minute;
            }
        });
        mButton1 = (Button)findViewById(R.id.normal_button);
        mButton1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(AlarmClockActivity.this, AlarmReceiver.class);
                //可以理解为延迟执行的intent，PendingIntent是对Intent一个包装
                //PendingIntent常和alermanger 和notificationmanager一起使用
                PendingIntent sender = PendingIntent.getBroadcast(AlarmClockActivity.this, 0, intent, 0);

                // 过10s 执行这个闹铃
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
                calendar.add(Calendar.SECOND, 10);

                // 进行闹铃注册
                AlarmManager manager = (AlarmManager)getSystemService(ALARM_SERVICE);
//                AlarmManager.RTC，硬件闹钟，不唤醒手机（也可能是其它设备）休眠；当手机休眠时不发射闹钟。
//                AlarmManager.RTC_WAKEUP，硬件闹钟，当闹钟发躰时唤醒手机休眠；
//                AlarmManager.ELAPSED_REALTIME，真实时间流逝闹钟，不唤醒手机休眠；当手机休眠时不发射闹钟。
//                AlarmManager.ELAPSED_REALTIME_WAKEUP，真实时间流逝闹钟，当闹钟发躰时唤醒手机休眠；

                manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, calendar.getTimeInMillis(), sender);

                Toast.makeText(AlarmClockActivity.this, "设置简单闹铃成功!", Toast.LENGTH_LONG).show();
            }
        });
        mButton2 = (Button)findViewById(R.id.repeating_button);
        mButton2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(AlarmClockActivity.this, AlarmReceiver.class);
                PendingIntent sender = PendingIntent.getBroadcast(AlarmClockActivity.this, 0, intent, 0);

                long firstTime = SystemClock.elapsedRealtime();	// 开机之后到现在的运行时间(包括睡眠时间)
                long systemTime = System.currentTimeMillis();

                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                calendar.setTimeZone(TimeZone.getTimeZone("GMT+8")); // 这里时区需要设置一下，不然会有8个小时的时间差
                calendar.set(Calendar.MINUTE, mMinute);
                calendar.set(Calendar.HOUR_OF_DAY, mHour);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);

                // 选择的每天定时时间
                long selectTime = calendar.getTimeInMillis();

                // 如果当前时间大于设置的时间，那么就从第二天的设定时间开始
                if(systemTime > selectTime) {
                    Toast.makeText(AlarmClockActivity.this, "设置的时间小于当前时间", Toast.LENGTH_SHORT).show();
                    calendar.add(Calendar.DAY_OF_MONTH, 1);
                    selectTime = calendar.getTimeInMillis();
                }

                // 计算现在时间到设定时间的时间差
                long time = selectTime - systemTime;
                firstTime += time;

                // 进行闹铃注册
                AlarmManager manager = (AlarmManager)getSystemService(ALARM_SERVICE);
                manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                        firstTime, 10*1000, sender);

                Log.i(this.getClass().getSimpleName(), "time ==== " + time + ", selectTime ===== "
                        + selectTime + ", systemTime ==== " + systemTime + ", firstTime === " + firstTime);

                Toast.makeText(AlarmClockActivity.this, "设置重复闹铃成功! ", Toast.LENGTH_LONG).show();
            }
        });

        mButtonCancel = (Button)findViewById(R.id.cancel_button);
        mButtonCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(AlarmClockActivity.this, AlarmReceiver.class);
                PendingIntent sender = PendingIntent.getBroadcast(AlarmClockActivity.this,
                        0, intent, 0);

                // 取消闹铃
                AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
                am.cancel(sender);
            }
        });
    }
}
